package com.chixing.day20_annotation_jdk8;

import com.zuoye.day19.Product;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ProductTest {
    public static void main(String[] args) {
        List<Product> list = new ArrayList<>();
        Product product1 = new Product("1","农夫山泉",2,"江苏", LocalDateTime.of(2022,8,3,13,23,12));
        Product product2 = new Product("2","荔枝",34,"江苏", LocalDateTime.of(2022,8,11,8,10,2));
        Product product3 = new Product("3","杯子",2335,"上海", LocalDateTime.of(2002,7,1,6,3,34));
        Product product4 = new Product("4","风扇",30,"江苏", LocalDateTime.of(2022,8,1,4,12,4));
        Product product5 = new Product("5","水杯",3333,"苏州", LocalDateTime.of(2007,8,3,13,23,12));
        Product product6 = new Product("6","荔枝1",34,"江苏", LocalDateTime.of(2022,8,11,8,10,2));
        Product product7 = new Product("7","农夫山泉1",2,"苏州", LocalDateTime.of(2022,8,3,13,23,12));
        Product product8 = new Product("8","荔枝2",34,"上海", LocalDateTime.of(2022,8,11,8,10,2));

        list.add(product1);
        list.add(product2);
        list.add(product3);
        list.add(product4);
        list.add(product5);
        list.add(product6);
        list.add(product7);
        list.add(product8);



        //（5）迭代器遍历该集合中的所有商品对象
        list.stream().forEach(System.out::println);
        //（6）使用foreach遍历该集合中的所有商品对象
        //（7）找出价格超过300的商品，将这些商品放置另一个集合ArrayList中
        System.out.println("===价格超过300的商品=====");
        list.stream().filter(p ->p.getProPrice() > 300).collect(Collectors.toList()).forEach(System.out::println);

        //（8）统计出个产地商品的数量（如 "苏州"-5, "上海"-10,"杭州"-6）
        System.out.println("===产地商品的数量===");
        Map map = list.stream().collect(Collectors.groupingBy(Product::getProCity,Collectors.counting()));
        map.forEach((k,v) -> System.out.println(k + "-" + v));

        // (9) 找出生产日期在"2009-10-1"前的商品，放置另一个集合ArrayList中

        System.out.println("=======");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        list.stream().filter(p ->p.getLocalDateTime().toLocalDate().isBefore(LocalDate.parse("2009-10-01",dateTimeFormatter)))
                .collect(Collectors.toList())
                .forEach(System.out::println);

    }
}
